<?php

$dir = '/storage/commentPhotos';

echo upload_process($dir);

function upload_process($dir) {
	$file = current($_FILES); // we handle the only file in time

	if($file['error'] == UPLOAD_ERR_OK) {
		//if(@move_uploaded_file($file['tmp_name'], "../../storage/commentPhotos/{$file['name']}"))
		$file['error']	= ''; //no errors, 0 - is our error code for 'moving error'
			
		$date = date('Y-m-d-g-i-s');
		$rand= rand(0, 1000);
		$image_name = $rand.$date.'.'.$extension;  

		$filename = stripslashes($file['name']);

		$extension= end(explode(".", $filename));
		$extension = strtolower($extension);

		$uploadedfile = $file['tmp_name'];
		$size=filesize($file['tmp_name']);

		if($extension=="jpg" || $extension=="jpeg" ){
			$src = imagecreatefromjpeg($uploadedfile);
		}
		else if($extension=="png"){
			$src = imagecreatefrompng($uploadedfile);
		}
		else{
			$src = imagecreatefromgif($uploadedfile);
		}

		list($width,$height)=getimagesize($uploadedfile);

		$newwidth=400;
		$newheight=($height/$width)*$newwidth;
		$tmp=imagecreatetruecolor($newwidth,$newheight);

		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

		$filename = "../.." . $dir . "/" . $image_name . $extension;
		
		imagejpeg($tmp,$filename,100);

		imagedestroy($src);
		imagedestroy($tmp);
	}

	$arr = array(
		'error' => $file['error'], 
		'file' => "{$dir}/" . $image_name . $extension,
		'tmpfile' => $file['tmp_name'], 
		'size' => $file['size']
	);

	if(function_exists('json_encode'))
	return json_encode($arr);

	$result = array();
	foreach($arr as $key => $val) {
		$val = (is_bool($val)) ? ($val ? 'true' : 'false') : $val;
		$result[] = "'{$key}':'{$val}'";
	}

	return '{' . implode(',', $result) . '}';
}
?>